package com.awesome.sqlitedemo;

import android.content.Context;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

import com.awesome.common.util.log.ShowLogUtil;


/**
 * Author: zhangjingfang
 * Email:zhangjingfang@jeejio.com
 * Date: 2022/7/8
 * Description:类描述
 */
public class DatabaseHelper extends SQLiteOpenHelper {
    public DatabaseHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    public DatabaseHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version, @Nullable DatabaseErrorHandler errorHandler) {
        super(context, name, factory, version, errorHandler);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        ShowLogUtil.info("创建数据库");
        String sql = "create table if not exists student (id integer primary key autoincrement," +
                "name varhcar(20)," +
                "age integer)";
        db.execSQL(sql);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (newVersion == 2) {
            String sql1 = "alter table student add grade integer";
            db.execSQL(sql1);
            String sql2 = "update student set name='Jane' where name='Alice'";
            db.execSQL(sql2);
            String sql3 = "create table if not exists result (id integer primary key autoincrement," +
                    "grade integer)";
            db.execSQL(sql3);
        }

    }
}
